package com.jann.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jann.entity.Signinfo;
import com.jann.entity.User;
import com.jann.entity.vo.PageInfoVo;
import com.jann.service.impl.SigninfoServiceImpl;
import com.jann.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/signinfo")
public class SigninfoController {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private SigninfoServiceImpl signinfoService;

    /**
     * 获取全部打卡记录
     *
     */
    @PostMapping("/findAll1")
    public Page<Signinfo> findAll1(@RequestBody PageInfoVo pageInfoVo) {
        final Page<Signinfo> page1 = new Page<>(
                pageInfoVo.getPage(),
                pageInfoVo.getSize());
        final QueryWrapper<Signinfo> wrapper = new QueryWrapper<>();
        wrapper.like("username", pageInfoVo.getUsername());
        wrapper.like("address", pageInfoVo.getAddress());
        if (pageInfoVo.getStartTime() != null && pageInfoVo.getEndTime() != null) {
            wrapper.between("date", pageInfoVo.getStartTime(), pageInfoVo.getEndTime());
        }
        return (Page<Signinfo>) signinfoService.page(page1, wrapper);
    }

    /**
    * 获取用户信息
    */

    @PostMapping("/findAll2")
    public Page<User> findAll2(@RequestBody PageInfoVo pageInfoVo) {
        final Page<User> page1 = new Page<>(
                pageInfoVo.getPage(),
                pageInfoVo.getSize());
        final QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("username", pageInfoVo.getUsername());
        return (Page<User>) userService.page(page1, wrapper);
    }


    /**
     * 用户打卡
     *
     */
    @PostMapping("/punch")
    public String Punch(@RequestBody Signinfo signinfo) {
        try {
            final boolean save = signinfoService.save(signinfo);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

}
